package io.jt808.proto.rsp;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import lombok.Data;

/**
 * 终端注册应答
 */
@Proto("8100")
@Data
public class JT8100 implements IProto {

    /**
     * 应答消息流水号
     */
    private short responseMessageSerialNo;
    /**
     * 结果
     * 0：成功；1：车辆已被注册；2：数据库中无该车辆；
     * 3：终端已被注册；4：数据库中无该终端
     */
    private byte result;

    /**
     * 权鉴码
     */
    private String authenticationCode;

    @Override
    public byte[] writeToBytes() {
        AutoBuffer buffer = new AutoBuffer();
        buffer.writeShort(responseMessageSerialNo);
        buffer.writeByte(result);
        if (result == 0)
            buffer.writeString(authenticationCode);
        return buffer.array();
    }

    @Override
    public void readFromBuf(AutoBuffer buffer) {
        responseMessageSerialNo = buffer.readShort();
        result = buffer.readByte();
        if (result == 0)
            authenticationCode = buffer.readString();
    }

//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                0x00, 0x01,
//                0x00,
//                0x00, 0x03, 0x00
//        };
//        AutoBuffer buffer = new AutoBuffer(bytes);
//        JT8100 jt8100 = new JT8100();
//        jt8100.readFromBuf(buffer);
//        System.out.println(new String(bytes).equals(new String(jt8100.writeToBytes())));
//    }
}